package com.study.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.study.pojo.User;
import com.study.service.UserService;
import com.study.util.JsonResult;

@Controller
public class UserController {
	@Autowired
	private UserService userService;
	
	@Value("#{settings.driver}")
	private String name;
	
	@RequestMapping("login")
	@ResponseBody  //不可缺少该注解
	public JsonResult login(User user,HttpServletRequest req) {
		System.out.println("================"+user);		
		JsonResult jsonResult = new JsonResult();
		jsonResult.setFlag(true);
		jsonResult.setObject(user);
		jsonResult.setMessage("success");
		return jsonResult;
	}
	
	@RequestMapping("user")
	public String userList(Model model,HttpServletRequest req) {
		List<User> userList = userService.getUserList();
		/*for(int i=100; i<105; i++){
			userList.add(new User(i,"zhangsan","男",i-80));
		}*/
		System.out.println(userList.toString());		
		model.addAttribute("userList", userList);
		return "user";
	}
	
	@RequestMapping("add")
	public String addUser(Model model,HttpServletRequest req) {
		User user = new User(57,"jack","男",18);
		userService.addUser(user);
		/*for(int i=100; i<105; i++){
			userList.add(new User(i,"zhangsan","男",i-80));
		}*/
		System.out.println(user);		
		
		return "redirect:/user";
	}
	
	@RequestMapping("test")
	public String test(Model model,HttpServletRequest req) {
		System.out.println("------------------test--------------------");		
		List<User> userList = userService.getUserList();
		/*for(int i=100; i<105; i++){
			userList.add(new User(i,"zhangsan","男",i-80));
		}*/
		System.out.println(userList.toString());		
		model.addAttribute("userList", userList);
		return "test";
	}
	
	@RequestMapping("test2")
	public String test2(Model model,HttpServletRequest req) {
		System.out.println(name +"------------------name--------------------");		
		
		return "test";
	}
	
	@RequestMapping("formIframe")
	public String formIframe(Model model,HttpServletRequest req) {
		System.out.println("------------------name--------------------"+req.getParameter("name"));
		System.out.println("------------------sex--------------------"+req.getParameter("sex"));
		System.out.println("------------------age--------------------"+req.getParameter("age"));
		
		model.addAttribute("status", "success");
		return "formIframe";
	}
	
	@RequestMapping("testIframe")
	@ResponseBody
	public JsonResult testIframe(Model model,HttpServletRequest req) {
		System.out.println("------------------name--------------------"+req.getParameter("name"));
		System.out.println("------------------sex--------------------"+req.getParameter("sex"));
		System.out.println("------------------age--------------------"+req.getParameter("age"));
		
		JsonResult jsonResult = new JsonResult();
		jsonResult.setFlag(true);
		jsonResult.setObject("111111111111111");
		jsonResult.setMessage("success");
		return jsonResult;
		/*model.addAttribute("status", "success");
		return "formIframe";*/
	}
}
